package cn.redsoft.wechatapi.handler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.redsoft.wechatapi.msg.req.generic.TextRequestMsg;
import cn.redsoft.wechatapi.msg.resp.passivity.NewsResponseMsg;
import cn.redsoft.wechatapi.msg.resp.passivity.NewsResponseMsg.Article;
import cn.redsoft.wechatapi.msg.resp.passivity.PassivityResponseMsg;
import cn.redsoft.wechatapi.msg.resp.passivity.TextResponseMsg;

public class TextMsgHandler implements RequestMessageHandler<TextRequestMsg> {

	@Override
	public PassivityResponseMsg doHandle(TextRequestMsg msg) {

		if (msg.getContent().contains("pic") || msg.getContent().contains("p")) {
			return replyNewsMsg(msg);
		}

		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date());
		TextResponseMsg responseMsg = new TextResponseMsg(
				msg.getFromUserName(), msg.getToUserName(),
				new Date().getTime(), msg.getContent() + " " + date);
		return responseMsg;
	}

	protected PassivityResponseMsg replyNewsMsg(TextRequestMsg msg) {
		NewsResponseMsg articleMsg = new NewsResponseMsg();
		List<Article> articleList = new ArrayList<Article>();
		Article article1 = new Article();
		article1.setTitle("微信公众帐号开发教程Java版");
		article1.setDescription("");
		// 将图片置为空
		article1.setPicUrl("");
		article1.setUrl("http://blog.csdn.net/lyq8479");

		Article article2 = new Article();
		article2.setTitle("第4篇\n消息及消息处理工具的封装");
		article2.setDescription("");
		article2.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg");
		article2.setUrl("http://blog.csdn.net/lyq8479/article/details/8949088");

		Article article3 = new Article();
		article3.setTitle("第5篇\n各种消息的接收与响应");
		article3.setDescription("");
		article3.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg");
		article3.setUrl("http://blog.csdn.net/lyq8479/article/details/8952173");

		Article article4 = new Article();
		article4.setTitle("第6篇\n文本消息的内容长度限制揭秘");
		article4.setDescription("");
		article4.setPicUrl("http://avatar.csdn.net/1/4/A/1_lyq8479.jpg");
		article4.setUrl("http://blog.csdn.net/lyq8479/article/details/8967824");

		articleList.add(article1);
		articleList.add(article2);
		articleList.add(article3);
		articleList.add(article4);

		articleMsg.setArticleCount(articleList.size());
		articleMsg.setArticles(articleList);
		articleMsg.setFromUserName(msg.getToUserName());
		articleMsg.setToUserName(msg.getFromUserName());
		articleMsg.setCreateTime(new Date().getTime());

		return articleMsg;
	}
}
